package com.tzxscu.hosdroolsnew.drools;
import com.alibaba.fastjson.JSONObject;
import com.tzxscu.hosdroolsnew.beans.*;
import com.tzxscu.hosdroolsnew.other.MyPath;
import org.kie.api.KieServices;
import org.kie.api.io.Resource;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.utils.KieHelper;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;


public class DroolsApi {
    private static Logger logger = LoggerFactory.getLogger(DroolsApi.class);
    @Async("pool1")
    public String getdrool(PatListInfo patListInfo)  {
        try {
            logger.info("输入---------------------------------------------------------");
            logger.info(JSONObject.toJSONString(patListInfo));
            // String rulepath = System.getProperty("user.dir")+"/rules/成都市市医保.drl";
            // String rulepath2 = System.getProperty("user.dir")+"/rules/四川省省医保.drl";
            String patient_id;
            String visit_id;
            String charge_type;
            String name;
            String insuplc_admdvs;
            String insuplc_class;
            ArrayList<Bills> bills = patListInfo.getBills();
            ArrayList<Diagnosis> diagnoses = patListInfo.getDiagnosis();
            ArrayList<Orders> orders = patListInfo.getOrders();
            patient_id = patListInfo.getPatient_id();
            visit_id = patListInfo.getVisit_id();
            charge_type = patListInfo.getCharge_type();
            name = patListInfo.getName();
            insuplc_admdvs = patListInfo.getInsuplc_admdvs();

//            String curProjectParent = MyPath.getRealPath();
////        String rulepath = curProjectParent+"/rules/成都市市医保.drl";
////        String rulepath2 = curProjectParent+"/rules/通用规则.drl";
//            String rulepath = "D:\\jar/rules/成都市市医保.drl";
//            String rulepath2 = "D:\\jar/rules/通用规则.drl";
//            System.out.println(rulepath);
//            Resource drl = ResourceFactory.newFileResource(rulepath);
//            Resource drl2 = ResourceFactory.newFileResource(rulepath2);
//            KieHelper helper = new KieHelper();
//            helper.addResource(drl, ResourceType.DRL);
//            helper.addResource(drl2, ResourceType.DRL);
//            KieSession kieSession = helper.build().newKieSession();
            KieServices ks = KieServices.Factory.get();
            KieContainer kieContainer = ks.getKieClasspathContainer();
            KieSession kieSession = kieContainer.newKieSession("rulesSession");
            List<RuleRet> ruleRets = new ArrayList<>();
            kieSession.setGlobal("rulerets", ruleRets);
            if (insuplc_admdvs.substring(0, 4).equals("5101")) {
                kieSession.getAgenda().getAgendaGroup("成都市医保").setFocus();
            } else {
                kieSession.getAgenda().getAgendaGroup("四川省医保").setFocus();
            }
            kieSession.insert(patListInfo);
            int count = kieSession.fireAllRules();
            // System.out.println("加载规则条数"+count);
            kieSession.dispose();
            //System.out.println(JSONObject.toJSONString(patListInfo));
            logger.info("输出---------------------------------------------------------");
            logger.info("加载规则条数"+count);
            logger.info(ruleRets.toString());
            return ruleRets.toString();
        }catch (Exception e){
            String ret="[{\"code\":-1,\"err_message\":"+e.getMessage().toString()+"}]";
            logger.error("异常输出-----------------------------------------------------------");
            logger.error(ret);
            return ret;
        }
    }

}
